home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / bbs_util / bsrc_260.zip / SRC.ZIP / BREC.C < prev    next >
C/C++ Source or Header  |  1996-02-20  |  7KB  |  255 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software, Co.                       */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          Freely Available<tm> Software.                 */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*              (C) Copyright 1987-96, Bit Bucket Software Co.              */
  11. /*                                                                          */
  12. /*                  This module was written by Bob Hartman                  */
  13. /*                                                                          */
  14. /*                 BinkleyTerm Batch Receiver State Machine                 */
  15. /*                                                                          */
  16. /*                                                                          */
  17. /*    For complete  details  of the licensing restrictions, please refer    */
  18. /*    to the License  agreement,  which  is published in its entirety in    */
  19. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.260.    */
  20. /*                                                                          */
  21. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  22. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  23. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  24. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  25. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  26. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  27. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  28. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  29. /*                                                                          */
  30. /*                                                                          */
  31. /* You can contact Bit Bucket Software Co. at any one of the following      */
  32. /* addresses:                                                               */
  33. /*                                                                          */
  34. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:343/491             */
  35. /* P.O. Box 460398                AlterNet 7:42/1491                        */
  36. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  37. /*                                Internet f491.n343.z1.fidonet.org         */
  38. /*                                                                          */
  39. /* Please feel free to contact us at any time to share your comments about  */
  40. /* our software and/or licensing policies.                                  */
  41. /*                                                                          */
  42. /*--------------------------------------------------------------------------*/
  43.  
  44. /* Include this file before any other includes or defines! */
  45.  
  46. #include "includes.h"
  47.  
  48. int BRInit (XMARGSP);
  49. int BREnd (XMARGSP);
  50. int BRTestSL (XMARGSP);
  51. int BRCheckSL (XMARGSP);
  52. int BRCheckFNm (XMARGSP);
  53. int BRCheckFile (XMARGSP);
  54. int BRFindType (XMARGSP);
  55.  
  56. typedef struct
  57. {
  58.     char *state_name;
  59.     int (*state_func) (XMARGSP);
  60. } BSTATES, *BSTATEP;
  61.  
  62. BSTATES Batch_Receiver[] =
  63. {
  64.     {"BRInit", BRInit},
  65.     {"BREnd", BREnd},
  66.     {"BR0", BRTestSL},
  67.     {"BR1", BRCheckSL},
  68.     {"BR2", BRCheckFNm},
  69.     {"BR3", BRCheckFile},
  70.     {"BR4", BRFindType},
  71. };
  72.  
  73. int
  74. BRInit (XMARGSP args)
  75. {
  76.     XON_DISABLE ();
  77.     args->filename = calloc (1, 13);
  78.     return ((int) args->control);
  79. }
  80.  
  81. int
  82. BREnd (XMARGSP args)
  83. {
  84.     free (args->filename);
  85.     return ((int) args->control);
  86. }
  87.  
  88. int
  89. BRTestSL (XMARGSP args)
  90. {
  91.     if (!no_sealink)
  92.         SENDBYTE (WANTCRC);
  93.  
  94.     args->T1 = timerset (1000);
  95.     args->T2 = timerset (12000);
  96.  
  97.     return (BR1);
  98. }
  99.  
  100. int
  101. BRCheckSL (XMARGSP args)
  102. {
  103.     long BR1Timer;
  104.  
  105.     BR1Timer = timerset (200);
  106.     while (!timeup (BR1Timer))
  107.     {
  108.         if (timeup (args->T2) || no_sealink)
  109.         {
  110.             args->result = Modem7_Receive_File (args->filename);
  111.             return (BR2);
  112.         }
  113.         if ((args->CHR = PEEKBYTE ()) >= 0)
  114.         {
  115.             return (BR4);
  116.         }
  117.         if (timeup (args->T1))
  118.         {
  119.             args->result = Modem7_Receive_File (args->filename);
  120.             return (BR2);
  121.         }
  122.         else
  123.         {
  124.             if (!CARRIER)
  125.                 return (CARRIER_ERR);
  126.             else
  127.                 time_release ();
  128.         }
  129.     }
  130.  
  131.     SENDBYTE (WANTCRC);
  132.     return (BR1);
  133. }
  134.  
  135. int
  136. BRCheckFNm (XMARGSP args)
  137. {
  138.     char buff1[20];
  139.     char *p;
  140.     int i;
  141.  
  142.     (void) memset (buff1, 0, 19);
  143.  
  144.     /* Was it the last file */
  145.     if (args->result == EOT_RECEIVED)
  146.     {
  147.         return (SUCCESS);
  148.     }
  149.     /* Did we get a valid filename */
  150.     else if (args->result == SUCCESS)
  151.     {
  152.         /* First set up the filename buffer */
  153.         p = buff1;
  154.         for (i = 0; i < 8; ++p, i++)
  155.         {
  156.             if (args->filename[i] != ' ')
  157.             {
  158.                 *p = args->filename[i];
  159.             }
  160.             else
  161.                 break;
  162.         }
  163.  
  164.         *p = '.';
  165.         ++p;
  166.         *p = '\0';
  167.         for (i = 8; i < 11; ++p, i++)
  168.         {
  169.             if (args->filename[i] != ' ')
  170.             {
  171.                 *p = args->filename[i];
  172.             }
  173.             else
  174.                 break;
  175.         }
  176.         *p = '\0';
  177.  
  178.         /* Rename .REQ files */
  179.  
  180.         i = strlen (buff1) - 4;
  181.         if ((i > 0) && (stricmp (&buff1[i], ".REQ") == 0))
  182.         {
  183.             buff1[i] = '\0';
  184.             status_line (MSG_TXT(M_REC_REQ_AS), buff1, buff1, TaskNumber);
  185.             sprintf (&buff1[i],".R%02x", TaskNumber);
  186.         }
  187.  
  188.         (void) strcpy (args->filename, buff1);
  189.  
  190.         args->result = Xmodem_Receive_File (args->path, args->filename);
  191.         return (BR3);
  192.     }
  193.     /* Otherwise, we have to exit */
  194.     else
  195.         return (args->result);
  196. }
  197.  
  198. int
  199. BRCheckFile (XMARGSP args)
  200. {
  201.     /* Was the file transfer good */
  202.     if ((args->result == SUCCESS) || (args->result == SUCCESS_EOT))
  203.     {
  204.         return (BR0);
  205.     }
  206.     else
  207.     {
  208.         return (args->result);
  209.     }
  210. }
  211.  
  212. int
  213. BRFindType (XMARGSP args)
  214. {
  215.     switch (args->CHR)
  216.     {
  217.     case SOH:
  218.     case SYN:
  219.         args->result = Batch_Xmodem_Receive_File (args->path, args->filename);
  220.         return (BR3);
  221.  
  222.     case EOT:
  223.         (void) TIMED_READ (0);
  224.         SENDBYTE (ACK);
  225.         return (SUCCESS);
  226.  
  227.         /*  NUL is a special case of "noise." We want to reset our timer if we
  228.    *  see one.
  229.    */
  230.  
  231.     case NUL:
  232.         args->T1 = timerset (2000);
  233.  
  234.         /*  Fall-through after resetting timer */
  235.  
  236.     default:
  237.         break;
  238.     }
  239.  
  240.     (void) TIMED_READ (0);
  241.     return (BR1);
  242. }
  243.  
  244. int
  245. Batch_Receive (char *where)
  246. {
  247.     XMARGS batch;
  248.     int res;
  249.  
  250.     batch.result = 0;
  251.     batch.path = where;
  252.     res = state_machine ((STATEP) Batch_Receiver, &batch, 2);
  253.     return (res);
  254. }
  255.